package com.sm.chatinputlibrary.emoji;

import android.content.Context;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sm.chatinputlibrary.R;
import java.util.ArrayList;
import java.util.List;

/**
 * 表情控件
 * created by zhud on 2019/4/2
 */
public class EmojiView extends LinearLayout implements ViewPager.OnPageChangeListener, EmojiPageAdapter.OnItemEmojiChildClickListener {
    private ViewPager mEmojiPage;
    private EmojiPageAdapter mEmojiPageAdapter;
    private List<EmojiPageBean> mEmojiPageBeans = new ArrayList<>();
    private LinearLayout mSelectIndexLayout;
    private List<ImageView> mDotImages = new ArrayList<>();
    private EmojiPageAdapter.OnItemEmojiChildClickListener onItemEmojiChildClickListener;

    public EmojiView(Context context) {
        this(context, null);
    }

    public EmojiView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public EmojiView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
        mEmojiPage = new ViewPager(context);
        mEmojiPage.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 0, 1));
        mEmojiPage.addOnPageChangeListener(this);
        mSelectIndexLayout = new LinearLayout(context);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 0, 0, 20);
        mSelectIndexLayout.setLayoutParams(params);
        mSelectIndexLayout.setGravity(Gravity.CENTER);
        addView(mEmojiPage);
        addView(mSelectIndexLayout);
        initEmoji();
    }

    //初始化表情
    private void initEmoji() {
        List<EmojiBean> emojiBeanList = EmojiManager.getInstance().getAllEmoji();
        List<EmojiBean> tempEmojiBean = null;
        for (int i = 0; i < emojiBeanList.size(); i++) {
            EmojiBean bean = emojiBeanList.get(i);
            if (i % 23 == 0 || tempEmojiBean == null) {
                if (tempEmojiBean != null) {
                    tempEmojiBean.add(null);
                }
                tempEmojiBean = new ArrayList<>();
                mEmojiPageBeans.add(new EmojiPageBean(tempEmojiBean));
            }
            tempEmojiBean.add(bean);
        }
        if (tempEmojiBean.size() < 24) {
            tempEmojiBean.add(null);
        }
        mEmojiPageAdapter = new EmojiPageAdapter(mEmojiPageBeans);
        mEmojiPageAdapter.setOnItemEmojiChildClickListener(this);
        mEmojiPage.setAdapter(mEmojiPageAdapter);
        initSelectDot();
    }

    private void initSelectDot() {
        LayoutParams params = new LayoutParams(20, 20);
        params.setMargins(15, 0, 15, 0);
        for (int i = 0; i < mEmojiPageBeans.size(); i++) {
            ImageView dot = new ImageView(this.getContext());
            dot.setLayoutParams(params);
            dot.setBackgroundColor(i == 0 ? R.drawable.input_dot_yellow : R.drawable.input_dot_yellow);
            mSelectIndexLayout.addView(dot);
            mDotImages.add(dot);
        }
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        onCheckedDot(i);
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    private void onCheckedDot(int index) {
        for (int i = 0; i < mDotImages.size(); i++) {
            ImageView dot = mDotImages.get(i);
            dot.setBackgroundColor(i == 0 ? R.drawable.input_dot_yellow : R.drawable.input_dot_yellow);
        }
    }

    @Override
    public void onItemChildClick(EmojiBean bean) {
        if (onItemEmojiChildClickListener != null) {
            onItemEmojiChildClickListener.onItemChildClick(bean);
        }
    }

    public void setOnItemEmojiChildClickListener(EmojiPageAdapter.OnItemEmojiChildClickListener onItemEmojiChildClickListener) {
        this.onItemEmojiChildClickListener = onItemEmojiChildClickListener;
    }
}
